package com.egg.auth.utils;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.egg.auth.config.ReadConfig;
import com.egg.common.core.exception.CustomException;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信工具类
 *
 */
@Slf4j
public class WechatUtils {


    /**
     * 获取 access_token
     *
     * @return
     */
    private static String getAccessToken() {
        String result = HttpUtil.post("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + ReadConfig.appletAppid() + "&secret=" + ReadConfig.appletAppsercret(), new JSONObject());
        JSONObject jsonResult = JSONObject.parseObject(result);
        String accessToken = jsonResult.getString("access_token");
        if (ObjectUtil.isEmpty(accessToken)) {
            log.error("获取AccessToken失败, 原因 {} 返回对象 {}", "accessToken为空", result);
            throw new CustomException("获取AccessToken失败");
        }
        return accessToken;
    }

    /**
     * 获取 openid
     *
     * @param code
     * @return
     */
    public static String getOpenid(String code) {
        String result = HttpUtil.get("https://api.weixin.qq.com/sns/jscode2session?appid=" + ReadConfig.appletAppid() + "&secret=" + ReadConfig.appletAppsercret() + "&js_code=" + code + "&grant_type=authorization_code");
        JSONObject jsonResult = JSONObject.parseObject(result);
        String openid = jsonResult.getString("openid");
        if (ObjectUtil.isEmpty(openid)) {
            log.error("获取openid失败, 原因 {}", "openid为空");
            throw new CustomException("获取openid失败");
        }
        return openid;
    }

    /**
     * 获取 用户手机
     *
     * @param code
     * @return
     */
    public static String getPhone(String code) {
        String accessToken = getAccessToken();
        Map<String, Object> params = new HashMap<>();
        params.put("code", code);
        String result = HttpUtil.post("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accessToken, params);
        JSONObject jsonResult = JSONObject.parseObject(result);
        JSONObject jsonPhone = JSONObject.parseObject(jsonResult.getString("phone_info"));
        if (ObjectUtil.isEmpty(jsonPhone)) {
            log.error("获取Phone失败, 原因 {} 返回对象 {} ", "phone_info为空", result);
            throw new CustomException("手机号获取失败");
        }
        String phone = jsonPhone.getString("phoneNumber");
        if (ObjectUtil.isEmpty(phone)) {
            log.error("获取Phone失败, 原因 {} 返回对象 {} ", "phone为空", result);
            throw new CustomException("手机号获取失败");
        }
        return phone;
    }

    /**
     * 获取 跳转小程序url
     *
     * @return
     */
    public static String getJumpUrlLink() {
        String accessToken = getAccessToken();
        String result = HttpUtil.post("https://api.weixin.qq.com/wxa/generate_urllink?access_token=" + accessToken, new HashMap<>());
        JSONObject jsonResult = JSONObject.parseObject(result);
        String urlLink = jsonResult.getString("url_link");
        if (ObjectUtil.isEmpty(urlLink)) {
            log.error("获取UrlLink失败, 原因 {} 返回对象 {} ", "urlLink为空", result);
            throw new CustomException("跳转链接获取失败");
        }
        return urlLink;
    }

}
